package com.yuwenchao.superafflatus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuwenchao.superafflatus.mapper.SuperDevProgressMapper;
import com.yuwenchao.superafflatus.mapper.SuperSalaryMapper;
import com.yuwenchao.superafflatus.mapper.SuperTableMapper;
import com.yuwenchao.superafflatus.pojo.entity.*;
import com.yuwenchao.superafflatus.service.SuperMusicService;
import com.yuwenchao.superafflatus.service.SuperUrlFavoriteService;
import com.yuwenchao.superafflatus.utils.RandomUtils;
import com.yuwenchao.superafflatus.utils.SysInfoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;

/**
 * @author chaooGG
 * @version 1.0
 * @date 2021/5/31 16:52
 * <p>
 * //没有加注解的url即所有用户角色均可访问    //菜单权限未开启
 * @PreAuthorize("hasRole('ROLE_admin')") //超级用户访问权限
 * @PreAuthorize("hasRole('ROLE_normal')") //正常用户访问权限
 */
@Slf4j
@Api(tags = "页面跳转")//角色权限控制
@Controller
public class ToSuperPageController {

    @Autowired
    SuperTableMapper superTableMapper;

    @Autowired
    SuperSalaryMapper superSalaryMapper;

    @Resource
    SuperDevProgressMapper progressMapper;

    @Autowired
    FunctionSystemController functionSystemController;

    // SuperCalculateController superCalculateController;


    @Autowired
    SuperDevProgressController superDevProgressController;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    SuperMusicService superMusicService;

    @Autowired
    SuperUrlFavoriteService superUrlFavoriteService;


    @ApiOperation(value = "去首页页面")
    @GetMapping("index")
    public String toIndexPage(Model model) {

        SuperLoginUser superLoginUserInfo = this.functionSystemController.getSuperLoginUserInfo();
        String username = superLoginUserInfo.getUsername();
        model.addAttribute("username",username);
        List<SuperMusic> musicList = superMusicService.list();
        model.addAttribute("musicList",musicList);
        return "index";
    }

    @ApiOperation(value = "去主页面")
    @GetMapping("welcomePage")
    public String toWelcomePage(Model model, HttpServletRequest request) throws ParseException {

        String ipAddress = SysInfoUtils.getIpAddress(request);
        model.addAttribute("ipAddress", ipAddress);
        int localPort = request.getLocalPort();//取得服务器端口
        model.addAttribute("localPort", localPort);

        //随机生成四位数
        String fourBitRandom = RandomUtils.getFourBitRandom();

        //随机生成六位数
        String sixBitRandom = RandomUtils.getSixBitRandom();

//        Map map = RandomUtils.survivalTime();
//        Integer days = (Integer) map.get("days");
//        Integer hours = (Integer) map.get("hours");
//        Integer minutes = (Integer) map.get("minutes");
//
//        model.addAttribute("days", days);
//        model.addAttribute("hours", hours);
//        model.addAttribute("minutes", minutes);
//        model.addAttribute("sixBitRandom", sixBitRandom);

        //来到首页展示redis存入的访问url请求次数

        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String welcomePage = ops.get("/index");  //获取访问首页次数
        if (welcomePage == null) {
            model.addAttribute("welcomePage", "1");
        } else {
            model.addAttribute("welcomePage", welcomePage);
        }
        String registPageStr = ops.get("/superLoginUser/register");

        if (registPageStr == null) {
            model.addAttribute("count", "30");
        } else {
            int registPage = Integer.parseInt(registPageStr)+30;
            model.addAttribute("count", registPage);
        }
        Set<String> keys = stringRedisTemplate.keys("*");
        if (keys == null) {
            model.addAttribute("size", "10");
        } else {
            int size = keys.size();
            model.addAttribute("size", size+10);
        }

        String downBookStr = ops.get("/downbook");
        String batchBookStr = ops.get("/BatchBookdown");

        int defBookDown=30;
        if(StringUtils.isBlank(downBookStr)&&StringUtils.isBlank(batchBookStr)){
            model.addAttribute("bookCount", defBookDown);
        }else if(StringUtils.isBlank(downBookStr)){
            int batchBooks =  Integer.parseInt(batchBookStr);
            model.addAttribute("bookCount", batchBooks+defBookDown);
        }else if(StringUtils.isBlank(batchBookStr)){
            int downBooks=  Integer.parseInt(downBookStr);
            model.addAttribute("bookCount", downBooks+defBookDown);
        }else if(!StringUtils.isBlank(downBookStr)&&!StringUtils.isBlank(batchBookStr)){
            int a = Integer.parseInt(downBookStr);
            int b =  Integer.parseInt(batchBookStr);
            int  bookCount =a+b+defBookDown;
            model.addAttribute("bookCount", bookCount);
        }

        return "page/main/welcome";
    }

    //默认访问路劲
    @GetMapping("/")
    public String view() {
        return "page/user/login";
    }


    @ApiOperation(value = "去网址收藏展示界面")
    @GetMapping("toURLPage")
    public String toURLPage( @RequestParam(value="condition",defaultValue="")String condition,Model model) {

        List<SuperUrlFavorite> common = superUrlFavoriteService.list(new QueryWrapper<SuperUrlFavorite>().eq("super_type",1).like("super_name",condition));
        List<SuperUrlFavorite> utils = superUrlFavoriteService.list(new QueryWrapper<SuperUrlFavorite>().eq("super_type",2).like("super_name",condition));
        List<SuperUrlFavorite> other = superUrlFavoriteService.list(new QueryWrapper<SuperUrlFavorite>().eq("super_type",3).like("super_name",condition));


        model.addAttribute("common",common);
        model.addAttribute("utils",utils);
        model.addAttribute("other",other);

        return "page/url/UrlFavorite";
    }


    @ApiOperation(value = "去网址收藏添加界面")
    @GetMapping("toAddURLPage")
    public String toAddURLPage() {

        return "page/url/addUrl";
    }

    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去删除网址界面")
    @GetMapping("todelURLPage")
    public String todelURLPage() {

        return "page/url/delUrl";
    }

    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去网址设置界面")
    @GetMapping("toManageUrlPage")
    public String manageUrl() {
        return "page/url/manageUrl";
    }

    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去网址编辑界面")
    @GetMapping("toEditURLPage")
    public String toEditURLPage(@RequestParam Integer id, Model model) {
        SuperUrlFavorite urlInfo = superUrlFavoriteService.getById(id);
        //Integer id1 = urlInfo.getId();
        String superName = urlInfo.getSuperName();
        String superUrl = urlInfo.getSuperUrl();
        String superDesc = urlInfo.getSuperDesc();
        String superIcon = urlInfo.getSuperIcon();
        Integer sort = urlInfo.getSort();
        Integer superType = urlInfo.getSuperType();


        model.addAttribute("id", id);
        model.addAttribute("superName", superName);
        model.addAttribute("superUrl", superUrl);
        model.addAttribute("superDesc", superDesc);
        model.addAttribute("superIcon", superIcon);
        model.addAttribute("superType", superType);
        model.addAttribute("sort", sort);

        return "page/url/editUrl";
    }

    @ApiOperation(value = "去登录页面")
    @GetMapping("loginPage")
    public String tologinPage() {

        return "page/user/login";
    }


    @ApiOperation(value = "去登录成功url")
    @GetMapping("/user/login")
    public String loginSuccess() {

        return "index";
    }

    @ApiOperation(value = "去注册页面")
    @GetMapping("registPage")
    public String toregisterPage() {

        return "page/user/regist";
    }

    @ApiOperation(value = "去403")
    @GetMapping("to403")
    public String to403() {

        return "error/403";
    }

    @ApiOperation(value = "去404")
    @GetMapping("to404")
    public String to404() {

        return "error/404";
    }

    @ApiOperation(value = "去500")
    @GetMapping("to500")
    public String to500() {

        return "error/500";
    }


    // @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去LayuiTable表格页面")
    @GetMapping("layuiTablePage")
    public String tolayuiTable() {

        return "page/table/layuiTable";
    }

    @ApiOperation(value = "去LayuiTable新增页面")
    @GetMapping("toAddPage")
    public String tolayuiTableAdd() {

        return "page/table/add";
    }


    @ApiOperation(value = "去数据表格编辑页面")
    @GetMapping("toEditPage")
    public String tolayuiTableEdit(@RequestParam Integer id, Model model) {
        //根据id查询这条数据
        SuperTable superTable = superTableMapper.selectById(id);
        //将需要的数据字段存进model中
        String superName = superTable.getSuperName();
        Integer superAge = superTable.getSuperAge();
        String superSex = superTable.getSuperSex();
        String superMobile = superTable.getSuperMobile();
        String superEmail = superTable.getSuperEmail();
        BigDecimal superSalary = superTable.getSuperSalary();
        String superAddress = superTable.getSuperAddress();
        String superIdCard = superTable.getSuperIdCard();
        String superZodiac = superTable.getSuperZodiac();
        String constellation = superTable.getConstellation();

        model.addAttribute("id", id);
        model.addAttribute("name", superName);
        model.addAttribute("sex", superSex);
        model.addAttribute("age", superAge);
        model.addAttribute("iphone", superMobile);
        model.addAttribute("email", superEmail);
        model.addAttribute("address", superAddress);
        model.addAttribute("salary", superSalary);
        model.addAttribute("superIdCard", superIdCard);
        model.addAttribute("superZodiac", superZodiac);
        model.addAttribute("constellation", constellation);
        //页面回显
        return "page/table/edit";
    }


    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去薪资列表页面")
    @GetMapping("toSalaryPage")
    public String toSalaryPage() {

        return "page/salary/salary";
    }


    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去薪资统计页面")
    @GetMapping("toCountPage")
    public String toCountPage(Model model) {

        return "page/salary/count";
    }


    // @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去文件上传页面")
    @GetMapping("toUploadPage")
    public String toUploadPage() {

        return "page/upload/upload";
    }

    //  @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去文件下载页面")
    @GetMapping("toDownloadPage")
    public String toDownloadPage() {

        return "page/download/download";
    }


    @ApiOperation(value = "去二维码功能页面")
    @GetMapping("toReqrcode")
    public String toReqrcodePage() {

        return "page/QRcode/QRcode";
    }

    @ApiOperation(value = "去多功能计算页面")
    @GetMapping("toCalcPage")
    public String toCalcPage() {

        return "page/calc/calc";
    }

    //@PreAuthorize("hasRole('ROLE_normal')")
    @ApiOperation(value = "去逻辑游戏页面")
    @GetMapping("toGamePage")
    public String toGamePage() {

        return "page/guess/game";
    }

    @ApiOperation(value = "去区域地址页面")
    @GetMapping("toAddressPage")
    public String toAddressPage() {

        return "page/address/area";
    }


    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去系统设置页面")
    @GetMapping("toPermissionPage")
    public String toPermissionPage() {

        return "page/security/permission";
    }




    @ApiOperation("去用户信息页面")
    @GetMapping({"toUserInfoPage"})
    public String toUserInfoPage(Model model) {
        SuperLoginUser superLoginUserInfo = this.functionSystemController.getSuperLoginUserInfo();
        Integer id = superLoginUserInfo.getId();
        String username = superLoginUserInfo.getUsername();
        String mobile = superLoginUserInfo.getMobile();
        String email = superLoginUserInfo.getEmail();
        String remark = superLoginUserInfo.getRemark();
        String avatar = superLoginUserInfo.getAvatar();
        String letter = superLoginUserInfo.getLetter();
        model.addAttribute("id", id);
        model.addAttribute("username", username);
        model.addAttribute("mobile", mobile);
        model.addAttribute("email", email);
        model.addAttribute("remark", remark);
        model.addAttribute("avatar", avatar);
        model.addAttribute("letter", letter);
        return "page/user/userInfo";
    }

    @ApiOperation("去密码修改页面")
    @GetMapping({"toUserPasswordPage"})
    public String toUserPasswordPage(Model model) {
        SuperLoginUser superLoginUserInfo = this.functionSystemController.getSuperLoginUserInfo();
        Integer id = superLoginUserInfo.getId();
        String username = superLoginUserInfo.getUsername();
        String password = superLoginUserInfo.getPassword();
        String avatar = superLoginUserInfo.getAvatar();
        String letter = superLoginUserInfo.getLetter();
        model.addAttribute("avatar", avatar);
        model.addAttribute("letter", letter);
        model.addAttribute("id", id);
        model.addAttribute("username", username);
        model.addAttribute("password", password);
        return "page/user/userPassword";
    }

    @ApiOperation(value = "去idea快捷键页面")
    @GetMapping("toIdeaKeyPage")
    public String toIdeaKeyPage() {

        return "page/ideakey/ideaKey";
    }

    @PreAuthorize("hasRole('ROLE_admin')")
    @ApiOperation(value = "去Dev日志页面")
    @GetMapping("toDevProPage")
    public String toDevProPage() {

        return "page/devlog/devlog";
    }


    @ApiOperation(value = "去Dev日志新增页面")
    @GetMapping("todevProAddPage")
    public String todevProAddPage() {

        return "page/devlog/addDevPro";
    }


    @ApiOperation(value = "去Dev日志编辑页面")
    @GetMapping("todevProEditPage")
    public String todevProEditPage(@RequestParam Integer id, Model model) {
        SuperDevProgress superDevProgress = progressMapper.selectById(id);
        //Integer id1 = superDevProgress.getId();.
        String progress = superDevProgress.getProgress();
        String problem = superDevProgress.getProblem();

        LocalDateTime createTimeT = superDevProgress.getCreateTime();
        /*DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String createTime = dtf2.format(createTimeT);*/
        String createTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(createTimeT);
        System.out.println(createTime);

        LocalDateTime updateTime = superDevProgress.getUpdateTime();
        String remark = superDevProgress.getRemark();

        model.addAttribute("id", id);
        model.addAttribute("progress", progress);
        model.addAttribute("problem", problem);
        model.addAttribute("createTime", createTime);
        model.addAttribute("updateTime", updateTime);
        model.addAttribute("remark", remark);

        return "page/devlog/editDevPro";
    }


    @ApiOperation(value = "去天气导航页面")
    @GetMapping("toWeatherPage")
    public String toWeatherPage() {

        return "page/weather/weather";
    }

    @ApiOperation(value = "去音乐列表页面")
    @GetMapping("toMusicPage")
    public String toMusicPage() {

        return "page/music/music";
    }

   @ApiOperation(value = "去音乐测试模板")
    @GetMapping("toMusicListPage")
    public String toMusicListPage(Model model) {

        List<SuperMusic> musicList = superMusicService.list();
        model.addAttribute("musicList",musicList);

        return "page/music/musicModel";
    }

    @ApiOperation(value = "去数字签名面板")
    @GetMapping("toSignaturePage")
    public String toSignaturePage() {

        return "page/signature/signature";
    }



}
